//
//  UTATag2SiteHelper.m
//  MallSearch
//
//  Created by David on 15/10/15.
//  Copyright (c) 2015年 VeryApps. All rights reserved.
/*
{
    "list":[
    {
        "sitename":"淘宝",
        "url":"https://www.maideyi.com/api/gourl.php?url=5200",
        "mobileurl":"http://c.maideyi.com/mdskdfwer23",
        "tags":[
                "淘宝",
                "淘宝网",
                "淘宝首页",
                ""
                ]
    }]
}

  */

#import "UTATag2SiteHelper.h"

#import "DataFetch.h"

static UTATag2SiteHelper *_tag2SiteHelper;

@interface UTATag2SiteHelper ()

@property (nonatomic, strong) NSArray *arrDictTagSites;

@end

@implementation UTATag2SiteHelper

+ (instancetype)shareTag2SiteHelper
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _tag2SiteHelper = [[UTATag2SiteHelper alloc] init];
    });
    return _tag2SiteHelper;
}

+ (void)setup
{
    [[UTATag2SiteHelper shareTag2SiteHelper] downloadTagsSite];
    [[UTATag2SiteHelper shareTag2SiteHelper] downloadHotKeywords];
}

+ (NSDictionary *)getSiteInfoWithTag:(NSString *)tag
{
    NSMutableCharacterSet *mcsTrim = [NSMutableCharacterSet whitespaceAndNewlineCharacterSet];
    [mcsTrim addCharactersInString:@"\r\n"];
    NSString *trimTag = [[[tag stringByTrimmingCharactersInSet:mcsTrim] stringByReplacingOccurrencesOfString:@" " withString:@""] stringByReplacingOccurrencesOfString:@" " withString:@""];
    if (0==trimTag.length) {
        return nil;
    }
    for (NSDictionary *dictSite in [UTATag2SiteHelper shareTag2SiteHelper].arrDictTagSites) {
        NSArray *arrTags = dictSite[@"tags"];
        for (NSString *tagItem in arrTags) {
            NSString *trimTagItem = [[[tagItem stringByTrimmingCharactersInSet:mcsTrim] stringByReplacingOccurrencesOfString:@" " withString:@""] stringByReplacingOccurrencesOfString:@" " withString:@""];
            if (NSOrderedSame==[trimTagItem compare:trimTag options:NSCaseInsensitiveSearch]) {
                return dictSite;
            }
        }
    }
    return nil;
}

+ (NSString *)getSiteUrlWithTag:(NSString *)tag
{
    NSDictionary *dictSite = [UTATag2SiteHelper getSiteInfoWithTag:tag];
    if (dictSite) {
        if (UI_USER_INTERFACE_IDIOM()==UIUserInterfaceIdiomPad) {
            return dictSite[@"url"];
        }
        else {
            return dictSite[@"mobileurl"];
        }
    }
    return nil;
}

- (void)downloadTagsSite
{
//    __weak typeof(self) wself = self;
//    [DataFetch getWithApi:@"" Dic:nil Completion:^(NSURLSessionDataTask *operation, id resultObj,NSString*link) {
//        if (!resultObj) {
//            return ;
//        }
//        if ([resultObj isKindOfClass:[NSDictionary class]]) {
//            
//            wself.arrDictTagSites = resultObj[@"list"];
//            return ;
//        }
//        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:resultObj options:0 error:nil];
//        wself.arrDictTagSites = dic[@"list"];
//    } Cache:^(id cacheObj) {
//        //        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:cacheObj options:0 error:nil];
//        if (!cacheObj) {
//            return ;
//        }
//        if ([cacheObj isKindOfClass:[NSDictionary class]]) {
//            
//            wself.arrDictTagSites = cacheObj[@"list"];
//            return ;
//        }
//        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:cacheObj options:0 error:nil];
//        
//        wself.arrDictTagSites = dic[@"list"];
//    }];
}

- (void)downloadHotKeywords
{
//    __weak typeof(self) wself = self;
//    [DataFetch getWithApi:@"" Dic:nil Completion:^(NSURLSessionDataTask *operation, id resultObj,NSString*link) {
//        if (!resultObj) {
//            return ;
//        }
//        if ([resultObj isKindOfClass:[NSDictionary class]]) {
//            NSString *keywords = resultObj[@"keywords"];
//            wself.arrHotKeywords = [keywords componentsSeparatedByString:@"|"];
//            return ;
//        }
//        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:resultObj options:0 error:nil];
//        NSString *keywords = dic[@"keywords"];
//        wself.arrHotKeywords = [keywords componentsSeparatedByString:@"|"];
//    } Cache:^(id cacheObj) {
//        if (!cacheObj) {
//            return ;
//        }
//        if ([cacheObj isKindOfClass:[NSDictionary class]]) {
//            NSString *keywords = cacheObj[@"keywords"];
//            wself.arrHotKeywords = [keywords componentsSeparatedByString:@"|"];
//            return ;
//        }
//        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:cacheObj options:0 error:nil];
//        NSString *keywords = dic[@"keywords"];
//        wself.arrHotKeywords = [keywords componentsSeparatedByString:@"|"];
//    } ];

}

@end
